package com.shiku.mianshi.controller;

import java.util.HashMap;
import java.util.Map;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.xyz.commons.utils.ReqUtil;
import cn.xyz.commons.utils.StringUtil;
import cn.xyz.commons.vo.JSONMessage;
import cn.xyz.mianshi.service.UserManager;
import cn.xyz.mianshi.service.impl.LiveRoomManagerImpl;
import cn.xyz.mianshi.utils.KSessionUtil;
import cn.xyz.mianshi.vo.LiveRoom;

@RestController
@RequestMapping("/liveRoom")
public class LiveRoomController {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private LiveRoomManagerImpl liveRoomManager;
	
	@RequestMapping(value = "/get")
	public JSONMessage getLiveRoom(@RequestParam(defaultValue="") String roomId) {
		Object data=null;
		try {
			LiveRoom room=liveRoomManager.get(new ObjectId(roomId));
			if(!room.getUrl().contains("//"))
				room.setUrl(KSessionUtil.getConfig().getLiveUrl()+room.getUrl());
			data=room;
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success(null,data);
	}
	
	@RequestMapping(value = "/list")
	public JSONMessage findLiveRoomList(@RequestParam(defaultValue="") String name,@RequestParam(defaultValue="") String nickName,
			@RequestParam(defaultValue="0") Integer userId,@RequestParam(defaultValue="0") Integer pageIndex,
			@RequestParam(defaultValue="10") Integer pageSize) {
		Object data=null;
		try {

			data=liveRoomManager.findLiveRoomList(name, nickName, userId, pageIndex, pageSize);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success(null,data);
	}
	
	
	@RequestMapping(value = "/create")
	public JSONMessage createLiveRoom(@ModelAttribute LiveRoom room) {
		Object data=null;
		try {
			room.setUserId(ReqUtil.getUserId());
			data=liveRoomManager.createLiveRoom(room);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success(null,data);
	}
	
	@RequestMapping(value = "/update")
	public JSONMessage updateLiveRoom(@ModelAttribute LiveRoom room) {
		Map<String,Object> data=new HashMap<String,Object>();
		try {
			liveRoomManager.updateLiveRoom(room);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success();
	}
	
	@RequestMapping(value = "/delete")
	public JSONMessage deleteLiveRoom(@RequestParam(defaultValue="")String roomId) {
		Map<String,Object> data=new HashMap<String,Object>();
		try {
			liveRoomManager.deleteLiveRoom(new ObjectId(roomId));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success();
	}
	
	@RequestMapping(value = "/findMemberList")
	public JSONMessage findLiveRoomMemberList(@RequestParam(defaultValue="") String roomId,@RequestParam(defaultValue="") String name,@RequestParam(defaultValue="") String nickName,
			@RequestParam(defaultValue="0") Integer userId,@RequestParam(defaultValue="10") Integer pageIndex,
			@RequestParam(defaultValue="0") Integer pageSize) {
		Object data=null;
		ObjectId id=null;
		try {
			if(!StringUtil.isEmpty(roomId))
				id=new ObjectId(roomId);
			data=liveRoomManager.findLiveRoomMemberList(id,name, nickName, userId, pageIndex, pageSize);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success(null,data);
	}
	
	@RequestMapping(value = "/enterInto")
	public JSONMessage enterIntoLiveRoom(@RequestParam(defaultValue="")String roomId) {
		Map<String,Object> data=new HashMap<String,Object>();
		try {
			liveRoomManager.enterIntoLiveRoom(ReqUtil.getUserId(), new ObjectId(roomId));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success();
	}
	@RequestMapping(value = "/quit")
	public JSONMessage exitLiveRoom(@RequestParam(defaultValue="")String roomId) {
		Map<String,Object> data=new HashMap<String,Object>();
		try {
			liveRoomManager.exitLiveRoom(ReqUtil.getUserId(), new ObjectId(roomId));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return JSONMessage.success();
	}
	
}
